<template>
    <div class="ToolBar">
        <a-dropdown>
            <div class="Select">
                <span>{{ indexNum }}</span> / <span>{{ paths.length }}</span>
            </div>
            <template #overlay>
                <a-menu @click="ScrollTo" class="ImageMenu">
                    <a-menu-item :key="index" v-for="(item, index) in paths.length">
                        <a href="javascript:;">{{ item }}</a>
                    </a-menu-item>
                </a-menu>
            </template>
        </a-dropdown>
        <a-slider class="slider" v-model:value="scale" :max="100" :min="10" :step="10" />
        <a-radio-group class="radio" v-model:value="SortType" name="radioGroup">
            <a-radio value="fileName">文件名</a-radio>
            <a-radio value="Time">修改时间</a-radio>
        </a-radio-group>
    </div>
    <a-layout-content style="margin: 8px">
        <div class="Content" ref="Content" @scroll="ChnageIndes">
            <a-image-preview-group>
                <a-image class="Image" :style="{ maxWidth: scale + '%' }" v-for="item in paths" :src="item" />
            </a-image-preview-group>
        </div>
    </a-layout-content>
</template>
<script lang="ts" setup>
import { ref, computed, nextTick, watch } from 'vue'
const props = defineProps(['Files']);
const indexNum = ref(0);
const Content = ref();
const SortType = ref('fileName');
const scale = ref(50)
const paths = ref([]);

const ScrollTo = function ({ key }) {
    const images = Content.value.getElementsByTagName('img');
    const img = images[key];
    const rect = img.getBoundingClientRect();
    Content.value.scrollTop += rect.top;
}

watch(() => props.Files, (val) => {
    const list = [];
    val.forEach(item => {
        list.push(item);
    });
    if (SortType.value == 'fileName') {
        list.sort((a, b) => a.name.localeCompare(b.name, "zh-Hans-CN"));
    } else if (SortType.value == 'updateTime') {
        list.sort((a, b) => a.lastModified - b.lastModified);
    }
    const _paths = [];
    for (let i = 0; i < list.length; i++) {
        const element = list[i];
        _paths.push(webkitURL.createObjectURL(element))
    }
    paths.value = _paths;
    ChnageIndes();
})

const ChnageIndes = function () {
    nextTick(() => {
        const images = Content.value.getElementsByTagName('img');
        let index = -1;
        for (let i = 0; i < images.length; i++) {
            const img = images[i];
            const rect = img.getBoundingClientRect();
            if (rect.bottom > 0) {
                index = i + 1
                break;
            }
        }
        indexNum.value = index;
    })
}





</script>
<style lang="less" scoped>
.Content {
    user-select: none;
    padding: 60px 12px 12px 12px;
    background: #fff;
    min-height: calc(100vh - 16px);
    max-height: calc(100vh - 16px);
    overflow: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    // transform: scale();
}

.ImageMenu {
    max-height: 400px;
    overflow-y: auto;
}

:deep(.Image) {
    display: block;
    max-width: 50%;
    margin: 0 auto;
}

:deep(.ant-image-mask) {
    background-color: transparent;
}

:deep(.ant-image-mask-info) {
    opacity: 0;
}

.ToolBar {
    display: flex;
    position: fixed;
    align-items: center;
    z-index: 10;
}

.slider {
    width: 160px;

}

.radio {
    padding-left: 20px;
}

.Select {
    padding: 4px 16px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.2);
    margin: 16px;
}
</style>